package com.hisense.hiioc.hismartplate.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @Auther:liaohongbing@hisense.com
 * @date:2019/1/3 20:49
 * @des
 */

@Component
public class ServerUtil implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {



    public int getPort() {
        return port;
    }

    public String getServer() {
        return server;
    }

    public String getBaseUrl() {
        return "http://"+getServer()+":"+getPort();
    }

    private int port;

    @Value("${server.ip}")
    private String server;

    @Override
    public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
        this.port = event.getEmbeddedServletContainer().getPort();
//        try {
//            this.server = InetAddress.getLocalHost().getHostAddress();
//        }catch (UnknownHostException e){
//            e.printStackTrace();
//        }
    }
}
